package com.zzw.zim.common.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtil {
    // 密钥必须是16/24/32位，对应AES-128/192/256
    private static final String KEY = "your16bitkeyhere";
    private static final String ALGORITHM = "AES";

    // 加密
    public static String encrypt(String data) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    // 解密
    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decrypted);
    }

    public static void main(String[] args) throws Exception {

        String encrypt = encrypt("{\"basinPlaneId\":\"9c961ccbf9f649269664bc18f229ec36\",\"lineName\":\"TEST\"}");
        String encrypt2 = encrypt("TEST");
        System.out.println(encrypt);
        System.out.println(encrypt2);


    }
}
